package com.beilang.hotline.company.manager.impl;

import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.CodeConstant;
import com.beilang.base.util.Constant;
import com.beilang.base.util.ReportUtil;
import com.beilang.base.util.StringUtil;
import com.beilang.hotline.company.dao.CompanyDAO;
import com.beilang.hotline.company.manager.CompanyMgr;
import com.beilang.hotline.company.model.CompanyModel;
import com.beilang.hotline.company.to.SearchTO;
import com.beilang.hotline.person.manager.PersonMgr;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WriteException;

import java.io.File;
import java.util.*;

public class CompanyMgrImpl implements CompanyMgr {
    private CompanyDAO companyDAO;
    private PersonMgr personMgr;
    private CodeLookupMgr codeLookupMgr;

    public void setPersonMgr(PersonMgr personMgr) {
        this.personMgr = personMgr;
    }

    public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
        this.codeLookupMgr = codeLookupMgr;
    }

    public void setCompanyDAO(CompanyDAO companyDAO) {
        this.companyDAO = companyDAO;
    }

    public CompanyModel createCompany(CompanyModel companyModel) {
        return companyDAO.createCompany(companyModel);
    }

    public CompanyModel saveCompany(CompanyModel companyModel) {
        return companyDAO.saveCompany(companyModel);
    }

    public void deleteCompany(Long companyId) {
        companyDAO.deleteCompany(companyId);
        personMgr.deleteAllCompanyPersons(companyId);
    }

    public CompanyModel getCompany(Long companyId) {
        CompanyModel companyModel = companyDAO.getCompany(companyId);
        if (companyModel != null) {
            companyModel.setCompanyServiceLevelDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_SERVICE_LEVEL_LIST, companyModel.getCompanyServiceLevel()));
            companyModel.setCustomAreaDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_CUSTOM_AREA_LIST, companyModel.getCustomArea()));
        }
        return companyModel;
    }

    public PaginationHelper getCompanyList(PaginationHelper helper, SearchTO searchTO) {
        int count = companyDAO.searchCount(searchTO);
        helper.setTotalSize(count);
        List list = companyDAO.searchList(helper, searchTO);
        if (list != null && list.size() > 0) {
            List l = new ArrayList();
            for (Object aList : list) {
                CompanyModel companyModel = (CompanyModel) aList;
                companyModel.setCompanyServiceLevelDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_SERVICE_LEVEL_LIST, companyModel.getCompanyServiceLevel()));
                companyModel.setCustomAreaDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_CUSTOM_AREA_LIST, companyModel.getCustomArea()));
                l.add(companyModel);
            }
            helper.setList(l);
        } else {
            helper.setList(list);
        }
        return helper;
    }

    public byte[] genereateCompanyReport(SearchTO searchTO) {
        byte[] buf = new byte[0];
        String fileName = "";
        try {
            fileName = getRandomFileName();
            File file = new File(Constant.FILEPATH + fileName);

            jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(file);
            jxl.write.WritableSheet ws = wwb.createSheet("企业", 0);
            writeCompanyExcelHeader(ws);
            List list = companyDAO.searchList(searchTO);

            if (list != null && !list.isEmpty()) {
                Iterator itor = list.iterator();
                int row = 1;
                while (itor.hasNext()) {
                    CompanyModel companyModel = (CompanyModel) itor.next();
                    writeCompanyExcelDetail(companyModel, ws, row);
                    row++;
                }
            }
            formatSheet(ws);

            wwb.write();
            wwb.close();
            ReportUtil reportUtil = new ReportUtil();
            buf = reportUtil.readByteArrayOfSpecifiedFile(Constant.FILEPATH + fileName);
            if (file.exists()) {
                file.delete();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return buf;
    }


    public void writeCompanyExcelDetail(CompanyModel companyModel, jxl.write.WritableSheet ws, int row) {
        try {
            if (companyModel != null) {
                companyModel.setCompanyServiceLevelDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_SERVICE_LEVEL_LIST, companyModel.getCompanyServiceLevel()));
                String customarea = codeLookupMgr.getCodeDesc(CodeConstant.CODE_CUSTOM_AREA_LIST, companyModel.getCustomArea());
                customarea = StringUtil.splitString(customarea);
                companyModel.setCustomAreaDesc(customarea);
            }
            jxl.write.Label label1 = new jxl.write.Label(0, row, String.valueOf(row), getCell(false));
            jxl.write.Label label2 = new jxl.write.Label(1, row, StringUtil.deNull(StringUtil.splitString(companyModel.getCompanyName())), getCell(false));
            jxl.write.Label label3 = new jxl.write.Label(2, row, StringUtil.deNull(companyModel.getCustomAreaDesc()), getCell(false));
            jxl.write.Label label4 = new jxl.write.Label(3, row, StringUtil.deNull(companyModel.getCompanyCode()), getCell(false));
            jxl.write.Label label5 = new jxl.write.Label(4, row, StringUtil.deNull(companyModel.getContactIp()), getCell(false));
            jxl.write.Label label6 = new jxl.write.Label(5, row, StringUtil.deNull(companyModel.getCompanyNumber()), getCell(false));
            jxl.write.Label label7 = new jxl.write.Label(6, row, StringUtil.deNull(companyModel.getCompanyServiceLevelDesc()), getCell(false));
            jxl.write.Label label8 = new jxl.write.Label(7, row, StringUtil.deNull(companyModel.getCompanyAddress()), getCell(false));
            jxl.write.Label label9 = new jxl.write.Label(8, row, StringUtil.deNull(companyModel.getCompanyRemarks()), getCell(false));
            jxl.write.Label label10 = new jxl.write.Label(9, row, StringUtil.deNull(companyModel.getCompanyMq()), getCell(false));

            String result ="";
            List list = companyModel.getAccessCompanyList();
            int i=0;
            if (list!=null&&!list.isEmpty())
            {
                Iterator itor  = list.iterator();
                while (itor.hasNext())
                {
                    String temp = (String)itor.next();
                    if  (i==0)
                    {
                      result= codeLookupMgr.getCodeDesc(CodeConstant.CODE_COMPANY_TYPE_LIST,temp);
                    }else
                    {
                        result= result+","+codeLookupMgr.getCodeDesc(CodeConstant.CODE_COMPANY_TYPE_LIST, temp);
                    }
                    i++;
                }
            }
           jxl.write.Label label11 = new jxl.write.Label(10, row, StringUtil.deNull(result), getCell(false));

            ws.addCell(label1);
            ws.addCell(label2);
            ws.addCell(label3);
            ws.addCell(label4);
            ws.addCell(label5);
            ws.addCell(label6);
            ws.addCell(label7);
            ws.addCell(label8);
            ws.addCell(label9);
            ws.addCell(label10);
            ws.addCell(label11);
        } catch (WriteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    public static void formatSheet(jxl.write.WritableSheet ws) {
        for (int i = 0; i < 11; i++) {
            if (i == 5 || i == 7 || i == 8) {
                ws.setColumnView(i, 100);
            } else if (i == 0) {
                ws.setColumnView(i, 10);
            } else {
                ws.setColumnView(i, 20);
            }

        }
    }


    public void writeCompanyExcelHeader(jxl.write.WritableSheet ws) {
        try {
            jxl.write.Label label1 = new jxl.write.Label(0, 0, "序号", getCell(true));
            jxl.write.Label label2 = new jxl.write.Label(1, 0, "企业名称", getCell(true));
            jxl.write.Label label3 = new jxl.write.Label(2, 0, "企业所在关区", getCell(true));
            jxl.write.Label label4 = new jxl.write.Label(3, 0, "企业代码", getCell(true));
            jxl.write.Label label5 = new jxl.write.Label(4, 0, "通讯IP", getCell(true));
            jxl.write.Label label6 = new jxl.write.Label(5, 0, "企业所有账册号", getCell(true));
            jxl.write.Label label7 = new jxl.write.Label(6, 0, "企业服务类别", getCell(true));
            jxl.write.Label label8 = new jxl.write.Label(7, 0, "企业地址", getCell(true));
            jxl.write.Label label9 = new jxl.write.Label(8, 0, "企业备注", getCell(true));
            jxl.write.Label label10 = new jxl.write.Label(9, 0, "企业MQ号", getCell(true));
            jxl.write.Label label11 = new jxl.write.Label(10, 0, "企业类型", getCell(true));

            ws.addCell(label1);
            ws.addCell(label2);
            ws.addCell(label3);
            ws.addCell(label4);
            ws.addCell(label5);
            ws.addCell(label6);
            ws.addCell(label7);
            ws.addCell(label8);
            ws.addCell(label9);
            ws.addCell(label10);
            ws.addCell(label11);
        } catch (WriteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    public String getRandomFileName() {
        Date cur = new Date();
        Calendar cld = Calendar.getInstance();
        cld.setTime(cur);
        int mth = cld.get(Calendar.MONTH) + 1;
        String nowStr = cld.get(Calendar.YEAR) + "" + mth + cld.get(Calendar.DATE) + cld.get(Calendar.HOUR) + cld.get(Calendar.MINUTE) + cld.get(Calendar.SECOND);
        Random rdm = new Random();
        String rdmNum = String.valueOf(rdm.nextInt());
        return nowStr + rdmNum + Constant.EXCEL_EXTENSION;
    }

    public WritableCellFormat getCell(boolean isBold) {

        WritableFont font = null;
        if (isBold) {
            font = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD);
        } else {
            font = new WritableFont(WritableFont.TIMES, 12, WritableFont.NO_BOLD);
        }


        try {
            font.setColour(Colour.BLACK);
        } catch (WriteException e1) {
            e1.printStackTrace();
        }
        WritableCellFormat format = new WritableCellFormat(font);
        try {
            format.setAlignment(jxl.format.Alignment.LEFT);
            format.setVerticalAlignment(jxl.format.VerticalAlignment.TOP);
            format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
        } catch (WriteException e) {
            e.printStackTrace();
        }
        return format;
    }


}
